﻿namespace Fotofly.PhotoViewer.SilverlightExample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    using Fotofly.PhotoViewer.FotoflyWebService;

    public partial class MainPage : UserControl
    {
        private double maxWidth;
        private double imageWidth;
        private double imageHeight;
        private string imageName;

        public MainPage()
        {
            this.InitializeComponent();

            this.maxWidth = SilverlightUtils.ReadResource<int>("maxWidth", true);
            this.imageName = SilverlightUtils.ReadResource<string>("imageName", true);

            WebServiceSoapClient webServiceSoapClient = new WebServiceSoapClient("WebServiceSoap", SilverlightUtils.ServerAddress + "/webservice.asmx");
            webServiceSoapClient.GetMetadataCompleted += new EventHandler<GetMetadataCompletedEventArgs>(this.GetMetadataCompleted);
            webServiceSoapClient.GetMetadataAsync(this.imageName);
        }

        void GetMetadataCompleted(object sender, GetMetadataCompletedEventArgs e)
        {
            // Check response is valid
            if (e.Error == null && e.Result != null && e.Result is PhotoMetadata)
            {
                PhotoMetadata photoMetadata = e.Result as PhotoMetadata;

                // Set Width
                this.imageWidth = this.maxWidth;
                this.Width = this.imageWidth;

                // Calculate and Set Height
                this.imageHeight = this.maxWidth * ((double)photoMetadata.ImageHeight / (double)photoMetadata.ImageWidth);
                this.Height = this.imageHeight;

                Uri imageUri = SilverlightUtils.GenerateLocalUri(imageName);

                PhotoPanel photoPanel = new PhotoPanel(this.imageWidth, this.imageHeight);
                photoPanel.ImagePanel.SetImageParams(imageUri);

                if (photoMetadata.MicrosoftRegionInfo.Regions.Length > 0)
                {
                    photoPanel.ImagePanel.SetRegionParams(photoMetadata.MicrosoftRegionInfo.Regions);
                    photoPanel.ShowPeopleControl = true;
                }

                // Get key from https://www.bingmapsportal.com/ and save in BingConfig.resx
                photoPanel.MapPanel.BingCredentialsProvider = BingConfig.CredentialsProvider;

                // Add a valid Gps Location
                if (photoMetadata.GpsPositionOfLocationShown.IsValidCoordinate)
                {
                    photoPanel.MapPanel.SetIconCoordinates(photoMetadata.GpsPositionOfLocationShown);
                }
                else if (photoMetadata.GpsPositionOfLocationCreated.IsValidCoordinate)
                {
                    photoPanel.MapPanel.SetIconCoordinates(photoMetadata.GpsPositionOfLocationCreated);
                }
                
                this.LayoutRoot.Children.Add(photoPanel);
            }
            else
            {
                throw new Exception("Webservice Call failed");
            }
        }
    }
}
